//
//  MCNavgationController.m
//  ZFGT
//
//  Created by apple on 2018/1/10.
//  Copyright © 2018年 allyoubank.com. All rights reserved.
//

#import "FLBNavgationController.h"
@interface FLBNavgationController ()

@property(nonatomic,strong)UIView *lineView;

@property (nonatomic, strong) UIColor *navBackgroundColor;
@property (nonatomic, strong) UIColor *navTitleColor;

@end

@implementation FLBNavgationController

- (void)dealloc
{
#if RELEASE
    
#else
    NSLog(@"%@ 销毁了",[self class]);
#endif
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUpNavConfig];
}


#pragma mark - 设置导航栏的背景色

- (UIColor *)navBackgroundColor {
    if (!_navBackgroundColor) {
        _navBackgroundColor = [UIColor clearColor];
    }
    return _navBackgroundColor;
}

- (UIColor *)navTitleColor {
    if (!_navTitleColor) {
        _navTitleColor = [UIColor whiteColor];
    }
    return _navTitleColor;
}

- (void)setNavStyle:(DesignNavBackgroundStyle)navStyle {
    _navStyle = navStyle;
    if (navStyle == DesignNavBackgroundStyle_Default) {
        self.navBackgroundColor = [UIColor clearColor];
        self.navTitleColor = [UIColor whiteColor];
    }
    else if (navStyle == DesignNavBackgroundStyle_White) {
        self.navBackgroundColor = [UIColor whiteColor];
        self.navTitleColor = MC_blackColor;
    }
}

- (void)setBackStyle:(BackActionStyle)backStyle{
    _backStyle = backStyle;
}

#pragma mark - hidden方法

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated {
    [self setNavigationBarHidden:hidden animated:animated navTitle:@""];
}

- (void)setNavigationBarHidden:(BOOL)hidden
                      animated:(BOOL)animated
                      navTitle:(NSString *)title {
    [self setNavigationBarHidden:hidden animated:animated navTitle:title rightItemImage:nil rightItemTitle:nil sel:nil];
}


- (void)setNavigationBarHidden:(BOOL)hidden
                      animated:(BOOL)animated
                      navTitle:(NSString *)title
                rightItemImage:(NSString *)itemImage
                rightItemTitle:(NSString *)itemTitle
                           sel:(SEL)sel {
    
    [super setNavigationBarHidden:hidden animated:animated];
    
    // 配置返回按钮
    if (self.viewControllers.count > 1) { // 不是首页时
        NSInteger count = self.viewControllers.count;
        UIViewController *VC = self.viewControllers[count - 1];
        
        if (!hidden) {
            UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:[self createBarleftImageAndTitle:@"nav_back" Title:title andSEL:@selector(goesBack)]];
            VC.navigationItem.leftBarButtonItem = leftItem;
        }
        else {
            [self creatNavigtionView:title rightItemImage:itemImage rightItemTitle:itemTitle sel:sel];
        }
        //修复navigationController侧滑pop失效的问题
        self.interactivePopGestureRecognizer.delegate = (id)VC;
    }
}


/// present出来的NAV设置返回按钮
- (void)presentedSetNavigationBar {
    // 没有
    if (!self.removeBackItem) {
        UIViewController *VC = self.viewControllers[0];
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:[self createBarleftImageAndTitle:@"nav_back" Title:@"" andSEL:@selector(goesBack)]];
        VC.navigationItem.leftBarButtonItem = leftItem;
    }
}


#pragma mark - 导航控制器的的返回按钮配置

- (void)creatNavigtionView:(NSString *)titleStr
            rightItemImage:(NSString *)rightItemImage
            rightItemTitle:(NSString *)rightItemTitle
                       sel:(SEL)sel{
    NSInteger count = self.viewControllers.count;
    UIViewController *VC = self.viewControllers[count - 1];
    UIView *View=[VC.view viewWithTag:88888];
    if (View) {
        [View removeFromSuperview];
    }

    CGRect navigationframe = self.navigationBar.frame;
    CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    UIView *navigtionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, navigationframe.size.width, navigationframe.size.height + statusBarHeight)];
    navigtionView.tag = 88888;
    navigtionView.backgroundColor = self.navBackgroundColor;
    // 获取当前的VC
    [VC.view addSubview:navigtionView];
    
    [navigtionView bringSubviewToFront:self.view];
    
    UIButton * goBackBtn = [self createBarleftImageAndTitle:@"nav_back" Title:@"" andSEL:@selector(goesBack)];
    goBackBtn.tintColor = self.navTitleColor;
    goBackBtn.frame = CGRectMake(15, statusBarHeight, 50, navigationframe.size.height);
    [navigtionView addSubview:goBackBtn];
    
    if (rightItemImage && rightItemTitle) {
         UIButton *rightBtn = [[UIButton alloc] init];
        rightBtn.frame = CGRectMake(ZD_screenHeight - 85, statusBarHeight, 80, navigationframe.size.height);
        [rightBtn setTitleColor:MC_blackColor forState:UIControlStateNormal];
        [rightBtn setImage:[UIImage imageNamed:rightItemImage] forState:UIControlStateNormal];
        [rightBtn setTitle:rightItemTitle forState:UIControlStateNormal];
        rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [rightBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -16, 0, 16)];
        [rightBtn setImageEdgeInsets:UIEdgeInsetsMake(0,60, 0, -60)];
        if (sel) {
            [rightBtn addTarget:VC action:sel forControlEvents:UIControlEventTouchUpInside];
        }
        rightBtn.tintColor = MC_blackColor;
        [navigtionView addSubview:rightBtn];
    
    }else if (rightItemImage || rightItemTitle) {
        UIButton *rightBtn = [[UIButton alloc] init];
        if (rightItemImage) {
            [rightBtn setImage:[UIImage imageNamed:rightItemImage] forState:UIControlStateNormal];
        }
        if (rightItemTitle) {
            [rightBtn setTitle:rightItemTitle forState:UIControlStateNormal];
            rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        }
        if (sel) {
            [rightBtn addTarget:VC action:sel forControlEvents:UIControlEventTouchUpInside];
        }
        rightBtn.tintColor = self.navTitleColor;
        rightBtn.frame = CGRectMake(ZD_screenWidth - 55, statusBarHeight, 50, navigationframe.size.height);
        [navigtionView addSubview:rightBtn];
    }
    
    
    UILabel *titleLable=[[UILabel alloc]init];
    titleLable.backgroundColor = [UIColor clearColor];
    titleLable.textColor = self.navTitleColor;
    titleLable.font = [UIFont systemFontOfSize:17];
    titleLable.text=titleStr;
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.frame=CGRectMake((ZD_screenWidth - 200) * 0.5, statusBarHeight, 200, navigationframe.size.height);
    [navigtionView addSubview:titleLable];
    titleLable.tag = 8080;
    
    if (self.navStyle == DesignNavBackgroundStyle_White) {
        UILabel *line = [[UILabel alloc] init];
      //  line.backgroundColor = FLB_backgroundColor; //FLB_backgroundColor
        line.frame = CGRectMake(0, navigtionView.frame.size.height-0.75, navigtionView.frame.size.width, 0.75);
        [navigtionView addSubview:line];
    }
}

/**
 设置导航栏标题 -- 使用于隐藏导航栏的页面，在页面加载之后再设置文字，比如webView加载完之后才设置标题
 
 @param title 标题文字
 */
- (void)setNavigationBarTitle:(NSString *)title {
    NSInteger count = self.viewControllers.count;
    UIViewController *VC = self.viewControllers[count - 1];
    UIView *navigtionView = [VC.view viewWithTag:88888];
    if (!navigtionView) {
        return;
    }
    UILabel *titleLabel = (UILabel *)[navigtionView viewWithTag:8080];
    if (!titleLabel) {
        return;
    }
    titleLabel.text = title;
}

- (UIEdgeInsets)mcEdgeInsetsSafeAreaInset
{
    UIEdgeInsets safeAreaInsets = UIEdgeInsetsZero;
    if (@available(iOS 11.0, *)) {
        safeAreaInsets = [UIApplication sharedApplication].delegate.window.safeAreaInsets;
        
    }else {
      //  safeAreaInsets =  UIEdgeInsetsMake(FLB_statusBarHeight, 0, 0, 0);
    }
    
    return safeAreaInsets;
}

- (UIButton *)createBarleftImageAndTitle:(NSString *)leftImage
                                   Title:(NSString *)title
                                  andSEL:(SEL)sel {
    
    NSDictionary *attribute = @{
                                NSFontAttributeName:[UIFont systemFontOfSize:19]
                                };
    CGSize buttonSize = [title sizeWithAttributes:attribute];
    UIButton * leftImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftImageBtn setImage:[UIImage imageNamed:leftImage] forState:UIControlStateNormal];
    [leftImageBtn setImage:[UIImage imageNamed:leftImage] forState:UIControlStateHighlighted];
    leftImageBtn.tintColor = self.navTitleColor;
    [leftImageBtn setTitle:title forState:UIControlStateNormal];
    leftImageBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    if (sel) {
        [leftImageBtn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    }
    [leftImageBtn setTitleColor: [UIColor colorWithRed:237.0f/255.0f green:126.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
    if (buttonSize.width<10) {
        leftImageBtn.frame = CGRectMake(0, 0, 25 + 30, 44);
    }else{
        leftImageBtn.frame = CGRectMake(0, 0, 25 + buttonSize.width, 44);
        
    }
    leftImageBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    return leftImageBtn;
}

//MARK: - 返回
- (void)goesBack{
    
    switch (_backStyle) {
        case BackActionStyle_Root:
            _backStyle=BackActionStyle_Default;
            [self popToRootViewControllerAnimated:YES];
            break;
        default:
            if (self.clickBackItemBlock) {
                self.clickBackItemBlock(nil);
                if (!_isWebView) {
                     self.clickBackItemBlock = nil;
                }
               
                return;
            }
            if  (_isWebView){
                return;
            }
            if (self.presentingViewController) {
                    // 当前的VC dismiss
                [self dismissViewControllerAnimated:YES completion:nil];
            } else {
                [self popViewControllerAnimated:YES];
            }
            break;
    }
   
}


//MARK: - 返回ROOt
- (void)goesRootVC{
    [self popToRootViewControllerAnimated:YES];
}

//MARK: - 相关配置设置
- (void)setUpNavConfig
{
    _lineColor = [UIColor redColor];    //默认颜色
    
    //去掉navigationBar 系统底部横线
    [self hiddenNavgationBarLineImgViewWithTranslucent:NO];
    
    //when translucent = NO ，起点为导航栏的下方,(当YES 时候，结合hiddenNavgationBarLineImgView方法)
    self.navigationBar.translucent = NO;
}

//MARK: - 去掉navigationBar 系统底部横
- (void)setLineColor:(UIColor *)lineColor
{
    _lineColor = lineColor;
    self.lineView.backgroundColor = _lineColor;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.navigationBar.frame.size.height - 0.5, self.view.frame.size.width, 0.5)];
    }
    return _lineView;
}

- (void)replaceSystemNavgationBarLine
{
    self.lineView.backgroundColor = _lineColor;
    [self.navigationBar addSubview:self.lineView];
}

- (void)setHiddenLine:(BOOL)hiddenLine
{
    self.lineView.hidden = hiddenLine;
}

- (void)setNavBarColor:(UIColor *)navBarColor
{
    self.navigationBar.barTintColor = navBarColor;
    
}

//去掉navigationBar 系统底部横线。 再配合 self.navigationBar.translucent = YES;，导航栏就会透明
- (void)hiddenNavgationBarLineImgViewWithTranslucent:(BOOL)translucent
{
    if (translucent) {
        [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        [self.navigationBar setShadowImage:[UIImage new]];
       
        return;
    }
    
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBg"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationBar setShadowImage:[UIImage new]];
}

- (void)navigationBarTranslucentWithImage:(UIImage *)navImg
{
    [self.navigationBar setShadowImage:navImg];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//MARK: - UINavigationControllerDelegate
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    [super pushViewController:viewController animated:YES];
}

// MARK: 不要调用NAV的preferredStatusBarStyle方法，而是调用VC的
- (UIViewController *)childViewControllerForStatusBarStyle{
    return self.topViewController;
}

#pragma mark - 设置右边的按钮
- (void)setrightNavBtn:(NSString *)title andRightImage:(NSString *)imageStr
{
    UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"主页" style:UIBarButtonItemStyleBordered target:self action:@selector(clickEvent)];
    self.navigationItem.rightBarButtonItem = myButton;
    
 
}


@end
